﻿using King.Box.Models.Db.Sys;
using King.Box.Models.Query.Sys;
using King.Box.Models.Util;
using King.Box.Models.View.Sys;
using King.Box.Service;
using King.Box.Utils.Global;
using King.Box.WebApi.Binder;
using King.Box.WebApi.Providers.Impl;
using PsCat.Aspet;
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.ModelBinding;

namespace King.Box.WebApi.Controllers
{
#if !DEBUG
    [ClientAuthorize]
#endif
    [RoutePrefix("api/sys")]
    public class SysController : ApiController
    {
        private readonly IDataService<Sys_User> _sysUserService;
        private readonly IDataService<Sys_Menu> _sysMenuService;
        private readonly ISysService _sysService;

        public SysController(ISysService sysService, IDataService<Sys_Menu> sysMenuService, IDataService<Sys_User> sysUserService)
        {
            _sysService = sysService;
            _sysMenuService = sysMenuService;
            _sysUserService = sysUserService;
        }

        #region 菜单
        [HttpGet]
        [UrlCat]
        [Route("menu/getlevel")]
        public IEnumerable<Sys_MenuData> SelectMenuLevel()
        {
            return _sysService.SelectMenuLevel();
        }

        [HttpGet]
        [UrlCat]
        [Route("menu/getall")]
        public IEnumerable<Sys_MenuData> GetMenuDetail()
        {
            return _sysService.SelectMenu();
        }

        [HttpGet]
        [UrlCat]
        [Route("menu/getpage")]
        public PageResult<Sys_MenuData> GetMenuByPage([ModelBinder(typeof(ModelBinderFromUri<SysMenuQuery>))]SysMenuQuery query)
        {
            query = query ?? new SysMenuQuery();
            return _sysService.SelectMenu(query);
        }

        [HttpGet]
        [UrlCat]
        [Route("menu/{id}")]
        public Sys_Menu GetMenu(int id)
        {
            return _sysMenuService.Get(id);
        }

        [HttpPost]
        [UrlCat]
        [Route("menu/add")]
        public bool AddMenu(Sys_Menu sysMenu)
        {
            if (sysMenu == null)
                throw new ArgumentNullException(nameof(sysMenu));

            {
                sysMenu.CreateTime = DateTime.Now;
                sysMenu.UpdateTime = DateTime.Now;
                sysMenu.IsDelete = (int)GlobalEnum.YN.No;
            }

            return _sysMenuService.AddEntity(sysMenu);
        }

        [HttpPost]
        [UrlCat]
        [Route("menu/update")]
        public bool UpdateMenu(Sys_Menu sysMenu)
        {
            if (sysMenu == null)
                throw new ArgumentNullException(nameof(sysMenu));

            {
                sysMenu.UpdateTime = DateTime.Now;
            }

            return _sysMenuService.Update(sysMenu);
        }
        #endregion

        #region 权限
        [HttpGet]
        [UrlCat]
        [Route("permission/getpage")]
        public PageResult<Sys_PermissionData> GetPermissionDetail([ModelBinder(typeof(ModelBinderFromUri<SysPermissionQuery>))]SysPermissionQuery query)
        {
            return _sysService.SelectPermission(query);
        }
        #endregion

        #region 用户
        [HttpGet]
        [UrlCat]
        [Route("user/getpage")]
        public PageResult<Sys_UserData> GetUserByPage([ModelBinder(typeof(ModelBinderFromUri<SysUserQuery>))]SysUserQuery query)
        {
            query = query ?? new SysUserQuery();
            return _sysService.SelectUser(query);
        }
        #endregion
    }
}
